package com.air.antispider.stream.dataprocess.businessprocess

import java.util.regex.{Matcher, Pattern}

import com.air.antispider.stream.dataprocess.constants.TravelTypeEnum
import com.air.antispider.stream.dataprocess.constants.TravelTypeEnum.TravelTypeEnum

object TravelTypeLabel {
  /**
    * 获取当前操作的往返类型
    * @param httpReferrer
    */
  def traveTypeLabel(httpReferrer: String):TravelTypeEnum = {
    //通过httpReferrer判断，单程只跟一个日期，往返跟两个
    //创建日期的正则匹配对象去与httpReferrer匹配
    val pattern: Pattern = Pattern.compile("(\\d{4})-(0\\d{1}|1[0-2])-(0\\d{1}|[12]\\d{1}|3[01])")
    val matcher: Matcher = pattern.matcher(httpReferrer)

    //对匹配的个数统计
    var count = 0
    while (matcher.find()) {
      val date: String = matcher.group()
      count = count + 1
    }

    //判断
    if (count == 1) {
      return TravelTypeEnum.OneWay
    }else if(count == 2) {
      return TravelTypeEnum.RoundTrip
    }
    TravelTypeEnum.Unknown
  }

}
